package com.ktian.android.compression.image;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

import android.graphics.BitmapFactory;

/**
 * 根据手机的分辨率和图片的大小获取图片合适的压缩比例
 * 
 * @author kTian
 *
 */
public class CompRatioUtill {
	
	public final static float DEFAULT_WIDTH = (1280f + 320f);	// 默认手机宽度
	public final static float DEFAULT_HEIGHT = (720f + 180f);	// 默认手机高度
	
	/**
	 * 处理网络图片压缩比, 默认手机分辨率
	 * 
	 * @param urlStr
	 * @return
	 * @throws IOException
	 */
	public static int getHttpCompRatioImageSampleSize(String urlStr) throws IOException{
		return getHttpCompRatioImageSampleSize(urlStr, DEFAULT_WIDTH, DEFAULT_HEIGHT);
	}
	
	public static int getHttpCompRatioImageSampleSize(String urlStr, float[] deviceSize) throws IOException{
		return getHttpCompRatioImageSampleSize(urlStr, deviceSize[0], deviceSize[1]);
	}

	/**
	 * 处理网络图片压缩比
	 * 
	 * @param urlStr
	 * @return
	 * @throws IOException
	 */
	public static int getHttpCompRatioImageSampleSize(String urlStr, float width, float height) throws IOException {
		URL url = new URL(urlStr);
		URLConnection conn = url.openConnection();
		conn.setConnectTimeout(60000);
		conn.connect();
		InputStream in = null;
		MyInputStream bis = null;
		in = conn.getInputStream();
		bis = new MyInputStream(in);
		// BufferedInputStream bis1= new BufferedInputStream(in);
		BitmapFactory.Options options = new BitmapFactory.Options();
		options.inJustDecodeBounds = true;

		BitmapFactory.decodeStream(bis, null, options);

		float WidthTimes = (float) ((float) options.outWidth / width);
		if (WidthTimes <= 0) {
			WidthTimes = 1;
		}
		float HeightTimes = (float) ((float) options.outHeight / height);
		if (HeightTimes <= 0) {
			HeightTimes = 1;
		}
		int be = Math.max(Math.round(WidthTimes), Math.round(HeightTimes));
		options.inSampleSize = be;
		return be;
	}
	
	/**
	 * 处理本地图片压缩比, 默认手机分辨率
	 * 
	 * @param urlStr
	 * @return
	 * @throws IOException
	 */
	public static int getNativeCompRatioImageSampleSize(String path) throws IOException {
		return getNativeCompRatioImageSampleSize(path, DEFAULT_WIDTH, DEFAULT_HEIGHT);
	}

	/**
	 * 处理本地图片压缩比
	 * 
	 * @param urlStr
	 * @return
	 * @throws IOException
	 */
	public static int getNativeCompRatioImageSampleSize(String path, float width, float height) throws IOException {
		InputStream in = null;
		MyInputStream bis = null;
		in = new FileInputStream(new File(path));
		bis = new MyInputStream(in);
		// BufferedInputStream bis1= new BufferedInputStream(in);
		BitmapFactory.Options options = new BitmapFactory.Options();
		options.inJustDecodeBounds = true;

		BitmapFactory.decodeStream(bis, null, options);

		float WidthTimes = (float) ((float) options.outWidth / width);
		if (WidthTimes <= 0) {
			WidthTimes = 1;
		}
		float HeightTimes = (float) ((float) options.outHeight / height);
		if (HeightTimes <= 0) {
			HeightTimes = 1;
		}
		int be = Math.max(Math.round(WidthTimes), Math.round(HeightTimes));
		options.inSampleSize = be;
		return be;
	}

}
